package cn.tedu.sims.controller;

import ch.qos.logback.core.net.HardenedObjectInputStream;
import cn.tedu.sims.mapper.LocationMapper;
import cn.tedu.sims.pojo.dto.*;
import cn.tedu.sims.pojo.entity.Location;
import cn.tedu.sims.pojo.entity.User;
import cn.tedu.sims.pojo.vo.ReportingUserVO;
import cn.tedu.sims.pojo.vo.UserRoleVO;
import cn.tedu.sims.service.IReportingUserService;
import cn.tedu.sims.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Validated
@RestController
@RequestMapping("/reportingusers")
@Api(tags = "01. 申报用户管理模块")
public class ReportingUserController {
    @Autowired(required = false)
    IReportingUserService reportingUserService;

    public ReportingUserController(){log.debug("创建控制器对象：ReportingUserController");}

    @ApiOperation("查询列表")
    @ApiOperationSupport(order = 410)
    @PreAuthorize("hasAuthority('/reportingusers')")
    @GetMapping("")
    public JsonResult<List<ReportingUserVO>> list() {
        log.debug("开始处理【查询列表】的业务,无参数");
        List<ReportingUserVO> list = reportingUserService.list();
        return JsonResult.ok(list);
    }

    @ApiOperation("查询回收站列表")
    @ApiOperationSupport(order = 420)
    @PreAuthorize("hasAuthority('/reportingusers/listStateId')")
    @GetMapping("/listStateId")
    public JsonResult<List<ReportingUserVO>> listStateId() {
        log.debug("开始处理【查询回收站列表】的业务,无参数");
        List<ReportingUserVO> list = reportingUserService.listStateId();
        return JsonResult.ok(list);
    }

    @ApiOperation("查询待审核列表")
    @ApiOperationSupport(order = 420)
    @PreAuthorize("hasAuthority('/reportingusers/CheckPending')")
    @GetMapping("/CheckPending")
    public JsonResult<List<ReportingUserVO>> listCheckPending() {
        log.debug("开始处理【查询待审核列表】的业务,无参数");
        List<ReportingUserVO> list = reportingUserService.listCheckPending();
        return JsonResult.ok(list);
    }

    @ApiOperation("模糊查询列表")
    @ApiOperationSupport(order = 430)
    @PreAuthorize("hasAuthority('/reportingusers/likeSelectUser')")
    @PostMapping("/likeSelectUser")
    public JsonResult<List<ReportingUserVO>> selectUserhaha(ReportingUserHaHaDTO userHaHa) {
        log.debug("开始处理【模糊查询列表】的业务{},参数",userHaHa);
        List<ReportingUserVO> likeList = reportingUserService.selectUserHaHa(userHaHa);
        return JsonResult.ok(likeList);
    }

    @ApiOperation("查询详情")
    @ApiOperationSupport(order = 440)
    @PreAuthorize("hasAuthority('/reportingusers/selectUserParticulars/id')")
    @PostMapping("/selectUserParticulars/{id}")
    public JsonResult<UserRoleVO> selectUserParticulars(@PathVariable Long id) {
        log.debug("开始处理【查询详情】的业务{},参数",id);
        UserRoleVO userRoleVO = reportingUserService.selectUserParticulars(id);
        return JsonResult.ok(userRoleVO);
    }

    @ApiOperation("修改密码")
    @ApiOperationSupport(order = 450)
    @PreAuthorize("hasAuthority('/reportingusers/updatePassword')")
    @PostMapping("/updatePassword")
    public JsonResult<Void> updatePassword(UpdatePasswordDTO updatePassword){
        log.debug("开始处理【修改用户密码】的业务{},参数",updatePassword);
        reportingUserService.updatePassword(updatePassword);
        return JsonResult.ok();
    }

    @ApiOperation("修改用户状态码")
    @ApiOperationSupport(order = 460)
    @PreAuthorize("hasAuthority('/reportingusers/updateStateId/id')")
    @PostMapping("/updateStateId/{id}")
    public JsonResult<Void> updateStateId(@PathVariable Long id){
        log.debug("开始处理【修改用户状态码】的业务{},参数",id);
        reportingUserService.updateStateId(id);
        return JsonResult.ok();
    }
    @ApiOperation("修改回收站用户状态码")
    @ApiOperationSupport(order = 470)
    @PreAuthorize("hasAuthority('/reportingusers/updateStateIda/id')")
    @PostMapping("/updateStateIda/{id}")
    public JsonResult<Void> updateStateIda(@PathVariable Long id){
        log.debug("开始处理【修改回收站用户状态码】的业务{},参数",id);
        reportingUserService.updateStateIda(id);
        return JsonResult.ok();
    }

    @ApiOperation("审核用户")
    @ApiOperationSupport(order = 490)
    @PreAuthorize("hasAuthority('/reportingusers/id/userUpdate')")
    @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "long")
    @PostMapping("/{id:[0-9]+}/userUpdate")
    public JsonResult<Void> userUpdate(@Range(min = 1, message = "审核用户失败") @PathVariable Long id){
        log.debug("开始处理【审核用户】的业务{},参数",id);
        reportingUserService.userUpdates(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除")
    @ApiOperationSupport(order = 480)
    @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "long")
    @PreAuthorize("hasAuthority('/reportingusers/id/delete')")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1, message = "删除用户失败,尝试删除用户失败") @PathVariable Long id){
        log.debug("开始处理[根据id删除用户]的请求,参数: {}", id);
        reportingUserService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改用户详情")
    @ApiOperationSupport(order = 490)
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "long")
    @PreAuthorize("hasAuthority('/reportingusers/id/update')")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> getStandardById(@PathVariable Long id,UserRoleDTO userRoleDTO) {
        log.debug("开始处理【根据id查询用户详情】的请求，参数：{},{}", id,userRoleDTO);
        reportingUserService.updateInfoById(id,userRoleDTO);
        return JsonResult.ok();
    }

    @ApiOperation("修改状态")
    @ApiOperationSupport(order = 500)
    @PreAuthorize("hasAuthority('/reportingusers/updateStatus')")
    @PostMapping("/updateStatus")
    public JsonResult<Void> updateStatus(UpdateStatusDTO updateStatusDTO){
        log.debug("开始处理【修改用户状态】的业务,参数:{}",updateStatusDTO);
        reportingUserService.updateStatus(updateStatusDTO);
        return JsonResult.ok();
    }

    @Autowired
    private  LocationMapper locationMapper;
    @ApiOperation("获取全部地区")
    @ApiOperationSupport(order = 500)
    @PreAuthorize("hasAuthority('/reportingusers/userListOptions')")
    @GetMapping("/userListOptions")
    @ResponseBody
    public JsonResult<List<Location>> userListOptions(){
        log.debug("开始处理【修改用户状态】的业务,无参数");
        Map<String,Object> map = new HashMap<>();
        List<Location> locationList = locationMapper.getLocationList(map);
        return JsonResult.ok(locationList);
    }

    @ApiOperation("驳回")
    @ApiOperationSupport(order = 520)
    @PreAuthorize("hasAuthority('/reportingusers/rejected')")
    @PostMapping("/rejected")
    public JsonResult<Void> rejected(ProjectRejectDTO projectRejectDTO){
        log.debug("开始处理【驳回】的业务,参数:{}",projectRejectDTO);
        reportingUserService.rejected(projectRejectDTO);
        return JsonResult.ok();
    }

    @PostMapping("/query")
    @ResponseBody
    public String query(Map<String,Object>  params){
//        List<Long> idList = (List<Long>) params.get("idList");
        return  null;
    }
}
